//
//  HomeCell.m
//  OCDemo
//
//  Created by 朱慕之 on 2022/1/3.
//

#import "HomeCell.h"

#import "ProductModel.h"
#import "DBShopCart.h"
#import "DBShopCartDetail.h"

@interface HomeCell ()

@property (strong, nonatomic) UIImageView   *icon;
@property (strong, nonatomic) UILabel       *title;
@property (strong, nonatomic) UILabel       *content;
@property (strong, nonatomic) UILabel       *price;
@property (strong, nonatomic) UIButton      *buyCarBtn;

@end

@implementation HomeCell

#pragma mark - CellHeight
+ (CGFloat)cellHeight {
    return kAutoWithSize(200);
}

#pragma mark - SetData

- (void)setProductModel:(ProductModel *)productModel {
    _productModel = productModel;
    _title.text = productModel.product_name;
    _content.text = productModel.product_description;
    
    UIImage *image = [UIImage imageNamed:productModel.product_iconimage];
    _icon.image = image;
    
    _price.attributedText = [NSMutableAttributedString productPriceText:_productModel.product_price marketPrice:_productModel.product_marketprice];
}

#pragma mark - Event

- (void)buyBtnMethod {
    
    NSLog(@"singleTableViewCellBuyBtnMethod:%@",_productID);
    
    NSDictionary *landingDic = [[kUserDefaultsSington valueForKey:kIsLoginArray] objectAtIndex:0];
    if (landingDic.count > 0) { //判断当前用户是否登录
        [BYToastTool makeToast:@"加入购物车成功" targetView:kKeyWindow delay:1.5];
        [[DBShopCart sharedInstance] LinkDatabaseAndAddToQueue];
        NSString *userID = [[[kUserDefaultsSington valueForKey:kIsLoginArray] objectAtIndex:0] valueForKey:@"user_name"];
        NSString *cartID =  [[DBShopCart sharedInstance] selectWithUser_Id:userID];
        [[DBShopCartDetail sharedInstance] LinkDatabaseAndAddToQueue];
        [[DBShopCartDetail sharedInstance] insetCart_Id:cartID WithProduct_Id:_productID WithNumber:@"1"];
        [[DBShopCartDetail sharedInstance] selectAllMethod];
    } else {
        [BYToastTool makeToast:@"您还没有登录，请先登录！" targetView:kKeyWindow delay:1.5];
    }
}

#pragma mark - Init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addSubview:self.icon];
        [self addSubview:self.title];
        [self addSubview:self.content];
        [self addSubview:self.price];
        [self addSubview:self.buyCarBtn];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [_icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kAutoWithSize(140), kAutoWithSize(140)));
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.mas_left).offset(kAutoWithSize(5));
    }];
    
    [_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-kAutoWithSize(15));
        make.left.equalTo(self.icon.mas_right).offset(kAutoWithSize(6));
        make.top.equalTo(self.mas_top).offset(kAutoWithSize(25));
        make.height.mas_equalTo(kAutoWithSize(15));
    }];
    
    [_content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.title.mas_bottom).offset(kAutoWithSize(7));
        make.right.equalTo(self.mas_right).offset(-kAutoWithSize(15));
        make.left.equalTo(self.icon.mas_right).offset(kAutoWithSize(6));
        make.height.mas_equalTo(kAutoWithSize(60));
    }];
    
    [_price mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(kAutoWithSize(15));
        make.left.equalTo(self.icon.mas_right).offset(kAutoWithSize(6));
        make.right.equalTo(self.buyCarBtn.mas_left).offset(-kAutoWithSize(20));
        make.bottom.equalTo(self.mas_bottom).offset(-kAutoWithSize(23));
    }];
    
    [_buyCarBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kAutoWithSize(37), kAutoWithSize(37)));
        make.right.equalTo(self.mas_right).offset(-kAutoWithSize(45));
        make.bottom.equalTo(self.mas_bottom).offset(-kAutoWithSize(20));
    }];
}


#pragma mark - LazyGet

- (UIImageView *)icon {
    if (!_icon) {
        _icon = [[UIImageView alloc] init];
    }
    return _icon;
}

- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.font = MZBoldFont(14);
        _title.textColor = MZRGBCOLOR(81, 81, 81);
    }
    return _title;
}

- (UILabel *)content {
    if (!_content) {
        _content = [[UILabel alloc] init];
        _content.textColor = MZRGBCOLOR(35, 35, 35);
        _content.font = MZFont(13);
        _content.numberOfLines = 3;    //行数最多三行
    }
    return _content;
}

- (UILabel *)price {
    if (!_price) {
        _price = [[UILabel alloc] init];
    }
    return _price;
}

- (UIButton *)buyCarBtn {
    if (!_buyCarBtn) {
        _buyCarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buyCarBtn setImage:[UIImage imageNamed:@"限时特卖界面购物车图标"] forState:UIControlStateNormal];
        [_buyCarBtn addTarget:self action:@selector(buyBtnMethod) forControlEvents:UIControlEventTouchUpInside];
    }
    return _buyCarBtn;
}

@end
